package org.fz.publisher.config;

import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

import javax.annotation.PostConstruct;

@Slf4j
// @RequiredArgsConstructor
@Configurable
public class MqConfig {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    // @PostConstruct
    // public void init(){
    //     // 每个RabbitTemplate只能配置一个ReturnCallback，因此我们可以在配置类中统一设置。
    //     rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback(){
    //         @Override
    //         public void returnedMessage(ReturnedMessage returned) {
    //             log.error("触发return callback");
    //             log.debug("exchange: {}",returned.getExchange());
    //             log.debug("routingKey: {}",returned.getRoutingKey());
    //             log.debug("message: {}",returned.getMessage());
    //             log.debug("replyCode: {}",returned.getReplyCode());
    //             log.debug("replyText: {}",returned.getReplyText());
    //         }
    //     });
    // }

    @PostConstruct
    public void init(){
        // 每个RabbitTemplate只能配置一个ReturnCallback，因此我们可以在配置类中统一设置。
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returned) {
                log.info("触发return callback,");
                log.info("exchange: {}", returned.getExchange());
                log.info("routingKey: {}", returned.getRoutingKey());
                log.info("message: {}", returned.getMessage());
                log.info("replyCode: {}", returned.getReplyCode());
                log.info("replyText: {}", returned.getReplyText());
            }
        });
    }
}
